<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>注册 - 电影网站</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/style.css">
</head>
<body class="bg-light">
<div class="container">
    <div class="row justify-content-center mt-5">
        <div class="col-md-6 col-lg-4">
            <div class="card shadow-sm">
                <div class="card-body">
                    <h3 class="card-title text-center mb-4">用户注册</h3>
                    <div id="errorMsg" class="alert alert-danger" style="display: none;"></div>
                    <form id="registerForm">
                        <div class="mb-3">
                            <label for="username" class="form-label">用户名</label>
                            <input type="text" class="form-control" id="username" name="username" required>
                            <div class="form-text">用户名长度为3-20个字符</div>
                        </div>
                        <div class="mb-3">
                            <label for="password" class="form-label">密码</label>
                            <input type="password" class="form-control" id="password" name="password" required>
                            <div class="form-text">密码长度至少为6个字符</div>
                        </div>
                        <div class="mb-3">
                            <label for="confirmPassword" class="form-label">确认密码</label>
                            <input type="password" class="form-control" id="confirmPassword" name="confirmPassword" required>
                        </div>
                        <div class="mb-3">
                            <label for="email" class="form-label">邮箱</label>
                            <input type="email" class="form-control" id="email" name="email" required>
                        </div>
                        <div class="d-grid gap-2">
                            <button type="submit" class="btn btn-primary">注册</button>
                            <a href="/login" class="btn btn-outline-secondary">返回登录</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.bundle.min.js"></script>
<script>
$(document).ready(function() {
    $('#registerForm').on('submit', function(e) {
        e.preventDefault();
        
        // 表单验证
        var username = $('#username').val();
        var password = $('#password').val();
        var confirmPassword = $('#confirmPassword').val();
        var email = $('#email').val();

        if (username.length < 3 || username.length > 20) {
            $('#errorMsg').text('用户名长度必须在3-20个字符之间').show();
            return;
        }

        if (password.length < 6) {
            $('#errorMsg').text('密码长度至少为6个字符').show();
            return;
        }

        if (password !== confirmPassword) {
            $('#errorMsg').text('两次输入的密码不一致').show();
            return;
        }

        if (!email.match(/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/)) {
            $('#errorMsg').text('请输入有效的邮箱地址').show();
            return;
        }

        $.ajax({
            url: '/doRegister',
            type: 'POST',
            data: {
                username: username,
                password: password,
                email: email
            },
            success: function(response) {
                if (response === 'success') {
                    window.location.href = '/login';
                } else {
                    $('#errorMsg').text(response).show();
                }
            },
            error: function() {
                $('#errorMsg').text('注册失败，请稍后重试').show();
            }
        });
    });
});
</script>
</body>
</html>